package stardust.product.platform.webc.spi.webservice.support;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.xml.ws.Endpoint;
import javax.xml.ws.spi.Provider;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;


public class WebServiceServer
{
  
  public static final int BLOCK_LENGTH = 512;
  
  private Provider provider;
  
  private Executor executor;
  
  private HttpServer httpServer;

  public WebServiceServer(String contextPath, int port, int threadCount) throws UnknownHostException, IOException
  {
    // 获取WebService的默认协议提供程序。
    this.provider = Provider.provider();
    
    /*
     * 创建并启动HTTP协议服务器，设定运行线程池和应用上下文。
     */
    this.httpServer = HttpServerProvider.provider().createHttpServer(new InetSocketAddress(port), WebServiceServer.BLOCK_LENGTH);
    this.executor = Executors.newFixedThreadPool(threadCount);
    this.httpServer.setExecutor(this.executor);
    httpServer.start();
  }
  
  public void publish(String contextPath, Object webServiceImpl)
  {
    Endpoint endpoint = Endpoint.create(webServiceImpl);
    endpoint.publish(this.httpServer.createContext(contextPath));
  }
  
}
